﻿#include "time.hpp"
#include <time.h>

#include <chrono>

std::string utils_time::time_format(const int64_t time, const std::string& format)
{
	char tmp[128] = { 0 };
	struct tm* t = gmtime((time_t*)&time);
	strftime(tmp, sizeof(tmp), format.c_str(), t);
	return std::string(tmp);
}

std::string utils_time::getCurrentSystemTime()
{
	auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
	struct tm* ptm = localtime(&tt);
	char date[60] = { 0 };
	sprintf(date, "%d-%02d-%02d",
		ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
	return std::string(date);
}

std::string utils_time::getUtcTime()
{
	auto now = std::chrono::system_clock::now();

	// 将时间点转换为time_t
	std::time_t now_c = std::chrono::system_clock::to_time_t(now);
	std::ostringstream oss;
	// 使用std::put_time来格式化时间输出
	oss << std::put_time(std::gmtime(&now_c), "%Y-%m-%d %H:%M:%S UTC");
	return oss.str();
}
